package com.holyn.guofang.view.my;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.holyn.guofang.R;
import com.holyn.guofang.model.sharepreference.UserInfoSharePreference;
import com.holyn.guofang.model.webhttp.HttpConfigure;
import com.holyn.guofang.model.webhttp.wo.StatusWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.guofang.presenter.my.MyContract;
import com.holyn.guofang.presenter.my.MyPresenter;
import com.holyn.guofang.utils.MD5Uitl;
import com.holyn.guofang.utils.ValidUtil;
import com.holyn.guofang.view.BaseActivity;
import com.holyn.guofang.view.Login.LoginActivity;
import com.vondear.rxtools.RxActivityUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/8/21.
 */

public class MyUpdatePasswordActivity extends BaseActivity implements BaseView, MyContract.UpdatePasswordView{
    private static final int REQUEST_ACTION_POST_NEW_PASSWORD_CODE = 0x0001;
    private static final int REQUEST_ACTION_POST_UPDATE_PASSWORD = 0x0002;

    @BindView(R.id.et_valid_code)
    EditText etValidCode;
    @BindView(R.id.btn_get_code)
    Button btnGetCode;
    @BindView(R.id.et_old_password)
    EditText etOldPassword;
    @BindView(R.id.et_new_password)
    EditText etNewPassword;
    @BindView(R.id.et_new_password_repeat)
    EditText etNewPasswordRepeat;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    MyContract.Presenter presenter;

    String oldPassword;
    String newPassword;
    String validCode;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_update_password;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);
        showToolbar("修改登录密码");

        presenter = new MyPresenter(this);
        presenter.addUpdatePasswordView(this);
    }

    @OnClick({R.id.btn_get_code, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_get_code:
                presenter.postNewPasswordCode(REQUEST_ACTION_POST_NEW_PASSWORD_CODE);
                break;
            case R.id.btn_submit:
                validCode = etValidCode.getText().toString().trim();
                if (TextUtils.isEmpty(validCode)){
                    toastMsg("请输入验证码");
                    return;
                }
                oldPassword = etOldPassword.getText().toString().trim();
                if (TextUtils.isEmpty(oldPassword)){
                    toastMsg("请输入旧密码");
                    return;
                }
                newPassword = etNewPassword.getText().toString().trim();
                if (TextUtils.isEmpty(newPassword)){
                    toastMsg("请输入新密码");
                    return;
                }

                if (!ValidUtil.isContainWord(newPassword) || !ValidUtil.isGreaterMinLength(8, newPassword)) {
                    showToastDialog("密码要求最少8位的字母与数字组合");
                    return;
                }

                String newPasswordRepeat = etNewPasswordRepeat.getText().toString().trim();
                if (TextUtils.isEmpty(newPasswordRepeat)){
                    toastMsg("请输入确认新密码");
                    return;
                }

                if (!newPassword.equals(newPasswordRepeat)) {
                    showToastDialog("新密码与确认新密码不一致");
                    return;
                }

                presenter.postUpdatePassword(REQUEST_ACTION_POST_UPDATE_PASSWORD, MD5Uitl.getMd5Value(oldPassword), MD5Uitl.getMd5Value(newPassword), validCode);

                break;
        }
    }

    @Override
    public void requestStart(Integer requestAction) {
        showLoadingDialog();
    }

    @Override
    public void requestError(Integer requestAction, Throwable e) {
        toastMsg("出现异常");
        closeLoadingDialog();
    }

    @Override
    public void requestComplete(Integer requestAction) {
        closeLoadingDialog();
    }

    @Override
    public void responseSuccess(Integer requestAction, StatusWo statusWo) {
        if (requestAction == REQUEST_ACTION_POST_NEW_PASSWORD_CODE){
            if (statusWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
                toastMsg("发送验证码成功");
            } else {
                toastMsg("发送验证码失败");
            }
        } else if (requestAction == REQUEST_ACTION_POST_UPDATE_PASSWORD){
            if (statusWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
                toastMsg("修改密码成功");
                UserInfoSharePreference.savePassword(this, newPassword);
                startActivity(new Intent(MyUpdatePasswordActivity.this, LoginActivity.class));
                RxActivityUtils.finishAllActivity();
            } else {
                toastMsg("修改密码失败");
            }
        }
    }
}
